﻿#CACHE{0}
jQuery(document).ready(function() {
	// Add product to cart via AJAX request
    jQuery("a.alohacart_add_product_link").live('click', function() {       
       pdk_add_loading_style(this); 
       jQuery.post('./?page=alohacart_ajax',
            {
                act: "add_product",
                id_article: jQuery(this).attr('id_article')
            },
            function(data){
               if (data) { //alert(data);
                    var cart_quantity = jQuery.evalJSON(data).cart_quantity;
                    var cart_price = jQuery.evalJSON(data).cart_price;
                    var not_enough_products = jQuery.evalJSON(data).not_enough_products;
                    jQuery(".alohacart_quantity_display").text(cart_quantity);
                    jQuery(".alohacart_price_display").text(cart_price);
                    update_cart();
                    pdk_remove_loading_style('a.alohacart_add_product_link');
                    if (not_enough_products)
                        show_dialog('not_enough_products');
                    else           
                        show_dialog('product_added');
               }		 
            });
       
	});
    
    // Clear cart via AJAX request
    jQuery("a.alohacart_clear_cart_link").live('click', function() {      
       pdk_add_loading_style(this);
       jQuery.post('./?page=alohacart_ajax',
            {
                act: "clear_cart"
            },
            function(data){
               if (data) { //alert(data);
                    if (data == 'DeleteOK') { 
                        jQuery(".alohacart_quantity_display").text('0');
                        jQuery(".alohacart_price_display").text('0');
                        update_cart();
                        pdk_remove_loading_style("a.alohacart_clear_cart_link");
                        //alert('<:alohacart:cart_empty:>');
                        show_dialog('cart_empty');
                    }
               }		 
            });
	});
    
    jQuery('.alohacart_cart_block .delete_link').live('click', function(){
        update_cart(jQuery(this).attr('data-id_product'), '', '');
    });
    
    jQuery('.alohacart_cart_block .quantity').live('<?php echo lire_config('alohacart/add_spinner')?'click':'change'; ?>', function(){
        update_cart('', jQuery(this).attr('data-id_product'), jQuery(this).val());
    });
    
    jQuery('.alohacart_cart_block .composite_quantity').live('<?php echo lire_config('alohacart/add_spinner')?'click':'change'; ?>', function(){
        update_cart('', '', '', '', jQuery(this).attr('data-id_composite'), jQuery(this).attr('data-id_product'), jQuery(this).val());
    });
    
    // Updates cart block via AJAX
    function update_cart(id_product_delete, id_product_change_quantity, product_change_quantity, id_shipping,
                         id_composite, id_composite_article, composite_quantity) {
        var id_shipping_from_form = jQuery('#form_alohacart_cart_order select#id_shipping option:selected').val();
        if (!id_shipping && id_shipping_from_form!=undefined) id_shipping = id_shipping_from_form;
        pdk_add_loading_style('.alohacart_cart_block');
        jQuery.post('#CHEMIN{formulaires/alohacart_cart_order/cart.php}', //?var_mode=calcul
            {
                id_product_delete: id_product_delete,
                id_product_change_quantity: id_product_change_quantity,
                product_change_quantity: product_change_quantity,
                id_shipping: id_shipping,
                id_composite: id_composite,
                id_composite_article: id_composite_article,
                composite_quantity: composite_quantity 
            },
            function(data){
               if (data) { //alert(data);
                   jQuery('.alohacart_cart_block').html(data);
                   refresh_cart_view();
                   update_balises();
                   refresh_shipping();
                   pdk_remove_loading_style('.alohacart_cart_block');
               }
            });
    }
    
    // Updated needed balises via AJAX (e.g. cart price)
    function update_balises() {
        jQuery.post('./?page=alohacart_ajax',
            {},
            function(data){
               if (data) { //alert(data);
                    if (data) {
                        var cart_quantity = jQuery.evalJSON(data).cart_quantity;
                        var cart_price = jQuery.evalJSON(data).cart_price;
                        jQuery(".alohacart_quantity_display").text(cart_quantity);
                        jQuery(".alohacart_price_display").text(cart_price);
                    }
                }		 
            }
        );
    }    
    
    // Shows or hides cart block according to number of products in the cart
    function refresh_cart_view() {
        if (jQuery('.alohacart_cart_block input#num_products').val()>0) {
            jQuery('.alohacart_cart_order_form_block').show();
            jQuery('.alohacart_cart_order_form_empty_block').hide();
        } else {
            jQuery('.alohacart_cart_order_form_block').hide();
            jQuery('.alohacart_cart_order_form_empty_block').show();
        }   
    }
    
    // Hiding and showing payment data
	/*
    jQuery('#form_alohacart_cart_order .payment_data').hide();
    jQuery('#form_alohacart_cart_order select#id_payment').live('click', function(){        
        jQuery('#form_alohacart_cart_order .payment_data').hide();        
        jQuery(this).siblings('.payment_data').show();
    });*/
    
    // When shipping method change
    jQuery('#form_alohacart_cart_order select#id_shipping').live('change', function(){
        update_shipping();
        //refresh_shipping();
    });
    
    update_shipping();
    function update_shipping() {
        id_shipping = jQuery('#form_alohacart_cart_order select#id_shipping option:selected').val();
        if (id_shipping != undefined)
            update_cart('','','',id_shipping);            
    }
    
    function refresh_shipping() {        
        id_shipping = jQuery('#form_alohacart_cart_order select#id_shipping option:selected').val();
        if (id_shipping != undefined) {
			pdk_add_loading_style('.alohacart_shipping_block');
            jQuery.post('#CHEMIN{formulaires/alohacart_cart_order/shipping.php}',
                {id_shipping:id_shipping},
                function(data) {
                    if (data) { //alert(data);
                        if (data) {
                            jQuery('#form_alohacart_cart_order .alohacart_shipping_block').html(data);
                            pdk_remove_loading_style('.alohacart_shipping_block');
                        }
                    }		 
                }
            );
		}
    }
	
	jQuery('.alohacart_recalculate_cart_link').live('click', function(){
		update_cart();
	})
	
	// Dialogs BEGIN
    
    jQuery('.alohacart_dialog_container a.close').live('click', function() {
        jQuery('.alohacart_dialog_container').each(function(){
            jQuery(this).fadeOut();
        });
    })
    
    $(window).bind("resize", function(){
		 	$(".alohacart_dialog_container").css("height", $(document).height());
    });
    // Dialogs END
    
});

function show_dialog(dialog_id) {	    
		if (jQuery('.alohacart_dialog_container_'+dialog_id).length == 0) {			
			jQuery('body').append('<div class="alohacart_dialog_container alohacart_dialog_container_'+dialog_id+'"></div>');            
            jQuery('.alohacart_dialog_container_'+dialog_id).load('#CHEMIN{inc/dialogs/load_dialog.php}?dialog='+dialog_id);
		}        
        jQuery('.alohacart_dialog_container_'+dialog_id).fadeIn();
	}